package com.fand.manager.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fand.common.exception.BusinessException;
import com.fand.manager.entity.SysMenu;
import com.fand.manager.entity.SysRoleMenu;
import com.fand.manager.mapper.SysMenuMapper;
import com.fand.manager.mapper.SysRoleMenuMapper;
import com.fand.manager.model.rolemenu.SysRoleMenuPostDTO;
import com.fand.manager.model.rolemenu.SysRoleMenuTreeDTO;
import com.fand.manager.service.ISysRoleMenuService;
import com.fand.manager.util.MenuTreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

/**
 * <p>
 * 角色菜单表 服务实现类
 * </p>
 *
 * @author javaboy
 * @since 2021-06-10
 */
@Slf4j
@Service
public class SysRoleMenuServiceImpl implements ISysRoleMenuService {

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysMenuMapper     sysMenuMapper;

    @Override
    public List<SysRoleMenuTreeDTO> findRoleTree(Long id) {
        if (Objects.isNull(id) || id < 1) {
            throw new BusinessException("非法参数");
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<SysMenu> sysMenus = sysMenuMapper.selectList(new QueryWrapper<>());
        stopWatch.stop();
        log.info("查询数据库耗时：{}" + stopWatch.getTotalTimeMillis());
        stopWatch.start();
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id", id));
        stopWatch.stop();
        log.info("查询数据库耗时：{}" + stopWatch.getTotalTimeMillis());
        stopWatch.start();
        List<Long> menuIds = sysRoleMenuList.stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()).collect(Collectors.toList());
        List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOList = sysMenus.stream().map(sysMenu ->
                SysRoleMenuTreeDTO.builder()
                        .id(sysMenu.getId())
                        .pid(sysMenu.getPid())
                        .title(sysMenu.getName())
                        .checked(menuIds.contains(sysMenu.getId()) && sysMenu.getType() == 1)
                        .spread(sysMenu.getType() == -1 ? true : false)
                        .build()
        ).collect(Collectors.toList());
        stopWatch.stop();
        log.info("查询数据库耗时：{}" + stopWatch.getTotalTimeMillis());
        stopWatch.start();
        List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOS = MenuTreeUtil.roleMenu2Tree(sysRoleMenuTreeDTOList);
        stopWatch.stop();
        log.info("构建树耗时：{}" + stopWatch.getTotalTimeMillis());
        return sysRoleMenuTreeDTOS;
    }

    @Override
    public Boolean save(SysRoleMenuPostDTO sysRoleMenuPostDTO) {
        if (Objects.isNull(sysRoleMenuPostDTO) || Objects.isNull(sysRoleMenuPostDTO.getRoleId())) {
            throw new BusinessException("非法参数");
        }
        Long roleId = sysRoleMenuPostDTO.getRoleId();
        List<SysRoleMenuTreeDTO> menus = sysRoleMenuPostDTO.getMenus();
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        if (CollUtil.isNotEmpty(menus)) {
            List<SysRoleMenuTreeDTO> sysRoleMenuTreeDTOS = MenuTreeUtil.tree2RoleMenu(menus);
            sysRoleMenuTreeDTOS.stream().forEach(sysRoleMenuTreeDTO -> {
                SysRoleMenu sysRoleMenu = SysRoleMenu.builder()
                        .roleId(roleId)
                        .menuId(sysRoleMenuTreeDTO.getId())
                        .build();
                sysRoleMenuMapper.insert(sysRoleMenu);
            });
        }
        return Boolean.TRUE;
    }


}
